package ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import common.PropertiesManager;

public class Display extends JFrame {

	private static final long serialVersionUID = 7447474756312301596L;
	private static JTextArea display = null;
	private static boolean debug = Boolean.parseBoolean(PropertiesManager.getProperty("Local", "Debug"));

	public Display() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		display = new JTextArea();
		display.setEditable(false);
		display.setWrapStyleWord(true);

		JScrollPane jScrollPane = new JScrollPane();
		jScrollPane.setViewportView(display);

		JPanel jContentPane = new JPanel();
		jContentPane.setLayout(new BorderLayout());
		jContentPane.add(jScrollPane, BorderLayout.CENTER);

		this.setSize(new Dimension(1000, 500));
		this.setContentPane(jContentPane);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.centrarPantalla();
	}

	private void centrarPantalla() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((dim.width - getSize().width) / 2, (dim.height - getSize().height) / 2);
	}

	public void write(String line) {
		if (display != null) {
			Date d = new Date();
			display.append(d.toString() + ": " + line + "\n");
			display.setCaretPosition(display.getDocument().getLength());
		}
	}
	
	public static void log(String line) {
		if (display != null) {
			Date d = new Date();
			display.append(d.toString() + ": " + line + "\n");
			display.setCaretPosition(display.getDocument().getLength());
		}
	}

	public static void rawLog(String line) {
		if (display != null) {
			display.append(line);
			display.setCaretPosition(display.getDocument().getLength());
		}
	}
	
	public static void debug(String line) {
		if (debug) {
			Date d = new Date();
			String debugLine = "[DEBUG]" + d.toString() + ": " + line + "\n";
			if (display != null) {
				display.append(debugLine);
				display.setCaretPosition(display.getDocument().getLength());
			}
			System.out.println(debugLine);
		}
	}
}
